package cn.invest.merry.module.invest.controller.admin.lrbase;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.invest.merry.framework.common.pojo.PageParam;
import cn.invest.merry.framework.common.pojo.PageResult;
import cn.invest.merry.framework.common.pojo.CommonResult;
import cn.invest.merry.framework.common.util.object.BeanUtils;
import static cn.invest.merry.framework.common.pojo.CommonResult.success;

import cn.invest.merry.framework.excel.core.util.ExcelUtils;

import cn.invest.merry.framework.apilog.core.annotation.ApiAccessLog;
import static cn.invest.merry.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.invest.merry.module.invest.controller.admin.lrbase.vo.*;
import cn.invest.merry.module.invest.dal.dataobject.lrbase.LrBaseDO;
import cn.invest.merry.module.invest.service.lrbase.LrBaseService;

@Tag(name = "管理后台 - 个股报告期利润")
@RestController
@RequestMapping("/invest/lr-base")
@Validated
public class LrBaseController {

    @Resource
    private LrBaseService lrBaseService;

    @PostMapping("/create")
    @Operation(summary = "创建个股报告期利润")
    @PreAuthorize("@ss.hasPermission('invest:lr-base:create')")
    public CommonResult<Long> createLrBase(@Valid @RequestBody LrBaseSaveReqVO createReqVO) {
        return success(lrBaseService.createLrBase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新个股报告期利润")
    @PreAuthorize("@ss.hasPermission('invest:lr-base:update')")
    public CommonResult<Boolean> updateLrBase(@Valid @RequestBody LrBaseSaveReqVO updateReqVO) {
        lrBaseService.updateLrBase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除个股报告期利润")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('invest:lr-base:delete')")
    public CommonResult<Boolean> deleteLrBase(@RequestParam("id") Long id) {
        lrBaseService.deleteLrBase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得个股报告期利润")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('invest:lr-base:query')")
    public CommonResult<LrBaseRespVO> getLrBase(@RequestParam("id") Long id) {
        LrBaseDO lrBase = lrBaseService.getLrBase(id);
        return success(BeanUtils.toBean(lrBase, LrBaseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得个股报告期利润分页")
    @PreAuthorize("@ss.hasPermission('invest:lr-base:query')")
    public CommonResult<PageResult<LrBaseRespVO>> getLrBasePage(@Valid LrBasePageReqVO pageReqVO) {
        PageResult<LrBaseDO> pageResult = lrBaseService.getLrBasePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LrBaseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出个股报告期利润 Excel")
    @PreAuthorize("@ss.hasPermission('invest:lr-base:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLrBaseExcel(@Valid LrBasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LrBaseDO> list = lrBaseService.getLrBasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "个股报告期利润.xls", "数据", LrBaseRespVO.class,
                        BeanUtils.toBean(list, LrBaseRespVO.class));
    }

}